package com.xingkeduo.business.transformation.json;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xingkeduo.pojo.dto.BaseDTO;
import com.xingkeduo.util.pojo.base.BaseJson;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.InputParamException;
import com.xingkeduo.util.tool.CommonUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/4/22 20:14
 **/
public class BaseDTOJTO extends BaseJson {


    /**
     * 成功处理返回json格式的数据
     *
     * @param content
     * @return
     */
    public static ObjectNode toSuccessJson(ObjectNode content) {
        ObjectNode root = MAPPER.createObjectNode();
        root.put("error", 0);
        root.put("reason", "0");
        root.put("content", content);
        return root;
    }

    /**
     * 处理失败返回json格式的数据
     *
     * @param content
     * @return
     */
    public static ObjectNode toErrorJson(String reason) {
        ObjectNode root = MAPPER.createObjectNode();
        root.put("error", -1);
        root.put("reason", reason);
        root.put("content", "");
        return root;
    }

    /**
     * 处理失败返回json格式的数据
     *
     * @param content
     * @return
     */
    public static ObjectNode toUserAuth(String reason) {
        ObjectNode root = MAPPER.createObjectNode();
        root.put("error", 9004);
        root.put("reason", reason);
        root.put("content", "用户信息更新中 请等待");
        return root;
    }

    /**
     * 成功处理返回json格式的数据
     *
     * @return
     */
    public static String toSuccessJson() {
        ObjectNode root = MAPPER.createObjectNode();
        root.put("error", 0);
        root.put("reason", "0");
        root.putPOJO("content", new ArrayList<>());
        return root.toString();
    }

    /**
     * 成功处理返回json格式的数据
     *
     * @return
     */
    public static String toSuccessCode(String code) {
        ObjectNode root = MAPPER.createObjectNode();
        root.put("error", 0);
        root.put("reason", "0");
        root.put("msg", "SUCCESS");
        root.put("html", code);
        return root.toString();
    }

    public static BaseDTO createQueryForPrimary(String json) throws Throwable {
        BaseDTO query = new BaseDTO();
        JsonNode root = validJson(json);
        if (root == null)
            throw new InputParamException(InputParamException.Type.INVALID, "参数");

        query.setId(validString(root, "companyid", 20, YesNoEnum.NO));

        return query;
    }

    /**
     * 返回实体信息
     *
     * @param table:
     * @return : com.fasterxml.jackson.databind.node.ObjectNode
     * @author : gaozifeng
     * @date : 2019年4月23日10:54:28
     */
    public static ObjectNode toJson(BaseDTO table) throws Throwable {
        ObjectNode root = MAPPER.createObjectNode();
        if (table != null) {

            if (null != table.getId()) {
                root.put("id", table.getId());
            }
            if (null != table.getName()) {
                root.put("name", table.getName());
            }
            if (0L != table.getCraftsmanId()) {
                root.put("craftsmanId", table.getCraftsmanId());
            }
            if (null != table.getIds() && table.getIds().size() > 0) {
                root.putPOJO("ids",table.getIds());
            }
            if (null != table.getServiceIds() && table.getServiceIds().size() > 0) {
                root.putPOJO("serviceListIds",table.getServiceIds());
            }
            if (null != table.getCode()) {
                root.put("code", table.getCode());
            }
            if (null != table.getValue()) {
                root.put("value", table.getValue());
            }
            if (null != table.getShopsServprodList() && table.getShopsServprodList().size() > 0) {
                root.putPOJO("serviceList", ShopServiceDTOJTO.toJson(table.getShopsServprodList()));
            }
            if (null != table.getSocialList()) {
                root.putPOJO("craftsmanList", CraftsmanSocialJTO.toJson(table.getSocialList()));
            }
            if (0L != table.getShopId()) {
                root.put("shopId", table.getShopId());
            }
            if (null != table.getShopCraftsmanServiceDtos()) {
                root.putPOJO("servicePriceList", ShopCraftsmanServiceDtoJTO.toJson(table.getShopCraftsmanServiceDtos()));
            }
        }
        return root;
    }

    public static ArrayNode toJson(List<BaseDTO> tables) throws Throwable {
        ArrayNode root = MAPPER.createArrayNode();

        if (!CommonUtil.isEmpty(tables))
            for (Iterator<BaseDTO> iterator = tables.iterator(); iterator.hasNext(); ) {
                root.add(toJson(iterator.next()));
            }

        return root;
    }
}
